/**
 * Created by yaqiangbi on 2017/6/13.
 */
$(document).ready(function () {
    $("#div1").click(function () {
        $(this).load("demo.txt #p1",function (responseTxt,statusTxt,xhr) {
            if (statusTxt=="success") alert(responseTxt);
            if (statusTxt=="error") alert("Error:"+xhr.status+":"+xhr.statusText);
        });
    });


    $("#postData").click(function () {
        // $.get("http://localhost:8181/ajax1?name=11",function (data,status) {
        //     alert("数据:"+data+"\n状态:"+status);
        // });
        $.post("http://localhost:8181/ajax2",
            {
            name:"W3Cschool教程",
            url:"http://www.w3cschool.cn"
            },
            function (data,status) {
                $("#div1").append("data")
                alert("数据:"+data+"\n状态:"+status);
             },"json");
    });


    $("#ajax").click(function () {
        $.ajax({"type":"post",url:"http://localhost:8181/ajax2",data:{name:"ajax请求"},dataType:"json",success:function (data,textStatus) {
            alert(data.name)
            $("div").html(result);
        },complete:function (XMLHttpRequest,textStatus) {

        },error:function (xhr,status,error) {

        }})
    });


    $("#get").click(function () {
        $.get("http://localhost:8181/ajax1",function (result) {
            alert(result)
        })
    });

    $("#getJson").click(function () {
        $.getJSON("http://localhost:8181/ajax1",function (data,status,xhr) {
           alert(data.detail);
        });
    });

    $("#getScript").click(function () {
        $.getScript("jQuery-AJAX-demo.js",function (response,status) {
            alert(status);
        })
    });


    personObj = new Object();
    personObj.firstName = "John";
    personObj.lastName = "Dome";
    personObj.age = 50;
    personObj.eyeColor = "blue";
    $("#param").click(function () {
        $("#div2").text($.param(personObj));
    });

    $("#ajaxDelegate").click(function () {
        $.get("http://localhost:8181/ajax1",function (result) {

        })
    });
    $(document).ajaxStart(function (event) {
        $("#div2").css("background-color","cyan");
        $("#div2").text("开始请求");
        alert("开始请求")
    });
    $(document).ajaxSend(function () {
        $("#div2").css("background-color","orange");
        $("#div2").text("将要开始请求");
        alert("将要开始请求")
    });
    $(document).ajaxSuccess(function () {
        setTimeout(function () {
            $("#div2").css("background-color","pink");
            $("#div2").text("请求成功");
        },1000)
        alert("请求成功")
    });
    $(document).ajaxError(function () {
        setTimeout(function () {
            $("#div2").css("background-color","black");
            $("#div2").text("请求错误");
        },1000)
        alert("请求错误")
    });
    $(document).ajaxComplete(function () {
        setTimeout(function () {
            $("#div2").css("background-color","yellow");
            $("#div2").text("请求完成");
        },2000)
        alert("请求完成")
    });

    $("#serialize").click(function () {
        $("#div2").text($("input").serialize());
    });

    $("#serializeArr").click(function () {
        var x = $("input").serializeArray();
        $.each(x,function (i,event) {
            $("#div2").append(event.name+":"+event.value+"  ")
        })
    });

    $("#data").click(function () {
        $("#div2").data("greeting","div");
    });
    $("#getData").click(function () {
        alert( $("#div2").data("greeting"))
    });
    $("#removeData").click(function () {
        $("#div2").removeData();
    });

    $("#getMethod").click(function () {
        x = $("p").get(0);
        alert(x.innerHTML)
        //alert(x.nodeName()+x.text())
    });
    
    $("p").click(function () {
        alert($(this).index())
    });
    $("#getIndex").click(function () {
        alert($(".index").index($('#p1')))
    });
    $("#getIndex").click(function () {
        x=$("p").toArray();
        for (i=0;i<x.length;i++){
            alert(x[i].innerHTML)
        }
    });

    $("#version").click(function () {
        var version = $().jquery;
        alert(version)
    });

});
